"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : reindex
Case Name   : 新建没索引的表,使用reindex重建数据库索引
Create At   : 2022/08/25
Owner       : @zou_jialiang050
Description :
    1.创建新数据库
    2.新库建表1并插入数据
    3.新库建表2并插入数据
    4.为表2创建索引
    5.使用reindex在线重建数据库索引
    6.清理环境
Expect      :
    1.创建新数据库成功
    2.建表1并插入数据成功
    3.建表2并插入数据成功
    4.成功
    5.成功,跳过表1
    6.清理环境成功
History     :
"""

import os
import unittest
from yat.test import Node
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.dbuserNode = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.table1 = 't_reindex_concurrency_0032_01'
        self.table2 = 't_reindex_concurrency_0032_02'
        self.index = 'i_reindex_concurrency_0032'
        self.database = 'd_reindex_concurrently_0032'
        self.Constant = Constant()

    def test_reindex(self):
        step1 = 'step1:创建数据库 expect:创建数据库成功'
        self.logger.info(step1)
        create_database = self.primary_sh.execut_db_sql(
            f'''create database {self.database};''')
        self.logger.info(create_database)
        self.assertIn('CREATE DATABASE', create_database, "执行失败" + step1)

        step2 = 'step2:新库建表1并插入数据 expect:建表1并插入数据成功'
        self.logger.info(step2)
        create_table = self.primary_sh.execut_db_sql(f'''
            drop table if exists {self.table1};
            create table {self.table1}(c_int int);
            insert into {self.table1} (select * from 
            generate_series(1,4000000));''', dbname=self.database)
        self.logger.info(create_table)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, create_table,
                      "执行失败" + step2)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, create_table,
                      "执行失败" + step2)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, create_table,
                      "执行失败" + step2)

        step3 = 'step3:新库建表2并插入数据 expect:建表2并插入数据成功'
        self.logger.info(step3)
        create_table = self.primary_sh.execut_db_sql(f'''
            drop table if exists {self.table2};
            create table {self.table2}(c_int int);
            insert into {self.table2} (select * from 
            generate_series(1,400000));''', dbname=self.database)
        self.logger.info(create_table)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, create_table,
                      "执行失败" + step3)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, create_table,
                      "执行失败" + step3)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, create_table,
                      "执行失败" + step3)

        step4 = 'step4:为表2创建索引 expect:成功'
        self.logger.info(step4)
        create_index = self.primary_sh.execut_db_sql(
            f'''drop index if exists {self.index};
                create index {self.index} on {self.table2}(c_int);''', 
            dbname=self.database)
        self.logger.info(create_index)
        self.assertIn(self.Constant.DROP_INDEX_SUCCESS_MSG, create_index,
                      "执行失败" + step4)
        self.assertIn(self.Constant.CREATE_INDEX_SUCCESS_MSG, create_index,
                      "执行失败" + step4)

        step5 = 'step5:使用reindex在线重建数据库索引 expect:成功,跳过表1'
        self.logger.info(step5)
        reindex = self.primary_sh.execut_db_sql(
            f'''reindex database concurrently {self.database};''',
            dbname=self.database)
        self.logger.info(reindex)
        self.assertIn('REINDEX', reindex, "执行失败" + step5)
        self.assertIn(f'table "public.{self.table2}" was reindexed', reindex,
                      '执行失败' + step5)
        self.assertNotIn(f'table "public.{self.table1}" was reindexed',
                         reindex, '执行失败' + step5)

    def tearDown(self):
        step6 = 'step6:清理环境 expect:清理环境成功'
        self.logger.info(step6)
        de_database = self.primary_sh.execut_db_sql(
            f'''drop database {self.database};''')
        self.logger.info(de_database)
        self.assertIn(self.Constant.DROP_DATABASE_SUCCESS, de_database,
                      "执行失败" + step6)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
